package com.Yourweibo;

import java.util.List;

import weibo4android.Count;
import weibo4android.Paging;
import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

import com.Yourweibo.adapter.TimeLineAdapter;
import com.Yourweibo.status.TimeLineConstant;

/**
 * @author root
 *
 */
public class UserTimelineActivity extends Activity {
 
	private ListView timeline;
	private Weibo weibo;
	private String userName;
	private List<Status> UserTimelineList;
	private Paging paging;
	private int WeiboPage=1;
	private TimeLineAdapter adapter;
	private int AlphaDurationMillis=500;
	private View headView;
	private List<Count> countList;
	private  Handler handler = new Handler() {
        public void handleMessage(Message message) {
        	switch(message.arg1){
        		case 1:
        			//timeline.removeHeaderView(headView);
        		//	headView.setVisibility(View.GONE);
        			 UserTimelineActivity.this.findViewById(R.id.timeline_list_headview_update_frame).setVisibility(View.VISIBLE);
    				 UserTimelineActivity.this.findViewById(R.id.timeline_list_headview_updateing_frame).setVisibility(View.GONE);
        			break;
        		
        	}
        	adapter.notifyDataSetChanged();  
        }
    };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.friend_timeline_layout);
		
		timeline=(ListView)this.findViewById(R.id.friend_timelinelistView1);
		//timeline 的 footview
		View footview=View.inflate(this, R.layout.timeline_list_footview, null);
		headView=View.inflate(UserTimelineActivity.this, R.layout.timeline_list_headview, null);
		
		
		headView.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 UserTimelineActivity.this.findViewById(R.id.timeline_list_headview_update_frame).setVisibility(View.GONE);
				 UserTimelineActivity.this.findViewById(R.id.timeline_list_headview_updateing_frame).setVisibility(View.VISIBLE);
				 new Thread() {
			            @Override
			            public void run() {
			            	 getCurrentStatus();
			            }
			        }.start();
				
			}});
		timeline.addHeaderView(headView);
		timeline.addFooterView(footview);
		
		
		weibo=TimeLineConstant.getInstance().getWeibo();
		
		
		paging=new Paging(WeiboPage);
		
		Bundle bundle=getIntent().getExtras();
		UserTimelineList=(List<Status>) bundle.getSerializable("Timeline");
		countList=(List<Count>) bundle.getSerializable("TimelineCount");
		userName=bundle.getString("UserName");
		adapter=new TimeLineAdapter(this, 0, UserTimelineList);
		timeline.setAdapter(adapter);
		setListAction(adapter);
	}
	/**
	 * 得到最近的微博list
	 * 
	 * ****/
	public void getCurrentStatus(){
		
		try {
			UserTimelineList=weibo.getUserTimeline(userName);
		
			Message message = handler.obtainMessage();
			message.arg1=1;
            handler.sendMessage(message);
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	} 
	
	/**
	 * 得到下一页微博list
	 * 
	 * */
	public void getNextPageStatus(){
		WeiboPage++;
		paging.setPage(WeiboPage);
		
		try {
			UserTimelineList.addAll(weibo.getUserTimeline(userName,paging));
		
			Message message = handler.obtainMessage();
            handler.sendMessage(message);
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public void setListAction(final TimeLineAdapter adapter){
		
		timeline.setOnScrollListener(new OnScrollListener(){

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				//当滚动到最后的时候
				if(view.getLastVisiblePosition()==totalItemCount-1){
					 new Thread() {
				            @Override
				            public void run() {
				        		getNextPageStatus();
				            }
				        }.start();
			
				 
				}
				if(view.getFirstVisiblePosition()==visibleItemCount){
					 
				}
				
			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
			
			}});
		
	}
	/*public void setAnimation(View view){
		AlphaAnimation translateAni=new AlphaAnimation(0, 1);
		translateAni.setDuration(AlphaDurationMillis);
		view.startAnimation(translateAni);
		
		
	}*/
	

}
